const DB = require('../tools/mysql')
const path = require('path')
const fs = require('fs')

module.exports = async ctx => {
    ctx.request.body = ctx.req.body ? ctx.req.body : ctx.request.body
    if (ctx.request.body.code == 1) {
        ctx.body = await DB('music')
    } else if (ctx.request.body.code == 2) {
        ctx.body = await DB('music').insert({ name: ctx.request.body.name, originalname: ctx.request.body.originalname, url: ctx.request.body.source })
    } else if (ctx.request.body.code == 3) {
        const isok = await DB('music').where('id', ctx.request.body.id).del()
        const fpath = path.join(__dirname, `../public/${ctx.request.body.url}`)
        if (isok === 1 && fs.existsSync(fpath)) {
            fs.unlinkSync(fpath)
        }
        ctx.body = isok
    }

}